package com.digitalchina.industry.controller;

import com.digitalchina.common.data.RtnData;
import com.digitalchina.industry.service.SceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fanbo on 2017-07-14.
 */
@Controller
@RequestMapping("/sceneController")
public class SceneController {

    @Autowired
    private SceneService sceneService;
    /**
     * 查询场景列表
     * @param pageSize
     * @param pageNum
     * @param sceneName
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.html")
    public String industrylList(@RequestParam(required = false, defaultValue = "5") long pageSize,
                                @RequestParam(required = false, defaultValue = "1") long pageNum,
                                @RequestParam(required = false) String sceneName, ModelMap map, HttpServletRequest request) {
        getList(sceneName,pageNum,pageSize,map);
        return "industry/scene_list";
    }

    /**
     * 场景增加
     * @param pageSize
     * @param pageNum
     * @param sceneClass
     * @param sceneNameAdd
     * @param sceneName
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/addIndustry.html")
    public String addIndustry(@RequestParam(required = false, defaultValue = "5") long pageSize,
                              @RequestParam(required = false, defaultValue = "1") long pageNum,
                              @RequestParam(required = false) String sceneClass,
                              @RequestParam(required = false) String sceneNameAdd,
                              @RequestParam(required = false) String sceneName, ModelMap map,HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sceneName", sceneNameAdd);
        params.put("sceneClass", sceneClass);
        sceneService.addIndustry(params);
        getList(sceneName,pageNum,pageSize,map);
        return "industry/scene_list";
    }

    /**
     * 场景更新
     * @param pageSize
     * @param pageNum
     * @param sceneName
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/editIndustry.html")
    public String editIndustry(@RequestParam(required = false, defaultValue = "5") long pageSize,
                               @RequestParam(required = false, defaultValue = "1") long pageNum,
                               @RequestParam(required = false) String sceneIdEdit,
                               @RequestParam(required = false) String sceneClassEdit,
                               @RequestParam(required = false) String sceneNameEdit,
                               @RequestParam(required = false) String sceneName, ModelMap map,HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sceneName", sceneNameEdit);
        params.put("sceneClass", sceneClassEdit);
        params.put("id", sceneIdEdit);
        sceneService.editIndustry(params);
        getList(sceneName,pageNum,pageSize,map);
        return "industry/scene_list";
    }

    /**
     * 场景删除
     * @param deleteId
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/deletIndustry.html")
    public RtnData deletIndustry(@RequestParam(required = false) String deleteId, ModelMap map, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", Integer.valueOf(deleteId));
        try {
            sceneService.deletIndustry(params);
        }catch (Exception e){
            e.printStackTrace();
            return RtnData.fail(true);
        }
        return  RtnData.ok(true);
    }




    public ModelMap getList(String sceneName,long pageNum,long pageSize,ModelMap map){

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sceneName", sceneName);
        int totalSize = sceneService.findTotalCount(params);
        params.put("startIndex", (pageNum - 1) * pageSize);
        params.put("pageS", pageSize);
        List scenes = sceneService.getAllIndustrylList(params);
        map.put("scenes", scenes);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("totelPage", totalSize % pageSize > 0 ? totalSize / pageSize + 1 : totalSize / pageSize);
        map.put("currentPage", pageNum);
        map.put("sceneName",sceneName);
        return map;
    }

    @RequestMapping(value = "/checkSceneName.html", method = RequestMethod.GET)
    @ResponseBody
    public RtnData checkSceneName(@RequestParam(required = true) String sceneName, ModelMap map){
        Map<String, Object> params = new HashMap<>();
        params.put("sceneName", sceneName);
        try {
            List<Map<String,Object>> rtnList = sceneService.checkSceneName(params);
            if(rtnList!=null && rtnList.size()>0){
                return RtnData.ok(false);
            }
        }catch (Exception e){
            e.printStackTrace();
            return RtnData.fail(true);
        }
        return  RtnData.ok(true);
    }
}
